package com.twu.biblioteca;


import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;

public class BibliotecaApp extends Exception{

    private static PrintStream printStream;
    private static InputStream inputStream;
    ArrayList<Movie> movies = new ArrayList();
    ArrayList<Book> myBooks = new ArrayList();
    private Login login;
    int menuOptions;
    Scanner scanner;
    private static String formatting = "%-25s %-15s %-25s %-25s";



    public BibliotecaApp(PrintStream printStream, InputStream inputStream){
        this.printStream = printStream;
        this.inputStream= inputStream;
        myBooks.add(new Book("Head First"));
        myBooks.add(new Book("Agile Methodologies"));
        myBooks.add(new Book("Information Systems"));


        movies.add(new Movie("Tsotsi", "2005", "Daniel Strydom", "7"));
        movies.add(new Movie("Die Hard","1988","John McTiernan ","5"));
        movies.add(new Movie("Sarafina","1992","Darrell Roodt","8"));
        movies.add(new Movie("When we were black","2006","Khalo Matabane","5"));

        login = new Login();
    }

    public void runAll(){
        while (true){
            displayMessageAndMenu();
            chooseFromMenu();
        }

    }

    public void displayMessageAndMenu() {

            printStream.println("Welcome to Biblioteca");
            printStream.println("1. View All Book\n2. Reserve Book\n3. View Profile\n4. View Movies\n5. End Program\n6. Login");
    }

    public void chooseFromMenu() {

        scanner = new Scanner(inputStream);
        menuOptions = scanner.nextInt();
        printStream.println(menuOptions);


            if(menuOptions==1){
                printMessage("View All Book:");
                viewBooks();
            }
            if(menuOptions==2){
                printMessage("Reserve Book:");
                checksIfLoggedInBeforeReservingBook();

            }
            if(menuOptions==3){
                printMessage("View Profile:");
                displayProfile();
            }
            if(menuOptions==4){
                printMessage("View Movies:");
                displayMovies();
            }
            if(menuOptions==5){
               printStream.println("Bye...");
               System.exit(0);
            }
            if(menuOptions==6){
                setUserCredentials();
            }
            if(menuOptions>6 || menuOptions<1){
                printMessage("Invalid option...");
            }

    }

    public void printMessage(String message) {
        printStream.println(message);
    }

    public void viewBooks() {
       printMessage("1. Head First\n2. Agile Methodologies\n3. Information Systems\n");
    }

    public void reserveBookMessage() {
       printMessage("Book has been reserved successfully\nThank You! Enjoy the book\n");
    }

    public void reserveBook() {
        scanner = new Scanner(inputStream);
        int bookChoice = scanner.nextInt();
        printStream.println(bookChoice);

        if(bookChoice==1 || bookChoice ==2 || bookChoice==3){
            if(myBooks.get(bookChoice-1).getReserved()){
                bookIsReserved();
            }
            else {
                myBooks.get(bookChoice-1).setReserved(true);
                reserveBookMessage();
            }
        }
    }

    public void bookIsReserved(){
        printMessage("Book is reserved");
    }

    public void displayProfileWhenLoggedIn() {
        getUserDetails();
        login.getUsername();
        login.getEmail();
        login.getPhoneNumber();
    }

    public void displayProfileWhenNotLoggedIn() {
        printMessage("Please talk to a Librarian.\nThank you.");
    }

    public void displayMovies() {
        String format = String.format(this.formatting, "Movie", "Year", "Director", "Rating");
        printStream.println(format);

        for(Movie movie: movies){
            printStream.println(String.format(BibliotecaApp.formatting, movie.toString().split("-")));
        }
        printStream.println();
    }

    public void setUserCredentials() {
        scanner = new Scanner(inputStream);
        printStream.println("Please enter your username");
        String username = scanner.next();

        printStream.println("Please enter your password");
        String password = scanner.next();
        login.setUsername(username);
        login.setPassword(password);
        login.getPassword();

        printStream.println(username + " "+ password + "\n");
    }

    public void getUserDetails(){
        String email = "mcylva@yahoo.com";
        String mobile ="0712340987";

        login.setEmail(email);
        login.setPhoneNumber(mobile);
        printMessage(login.getUsername());
        printMessage(email);
        printMessage(mobile);
    }

    public void displayProfile() {
        if(login.getUsername().isEmpty()){
            displayProfileWhenNotLoggedIn();
        }
        else{
            displayProfileWhenLoggedIn();
        }
    }

    public void checksIfLoggedInBeforeReservingBook() {
        if(login.getUsername().isEmpty()) {
            printMessage("Please log in to reserve book");
        }
        else {
            viewBooks();
            reserveBook();
        }
    }
}